import networkx as nx
import numpy as np
import matplotlib.pyplot as plt

class Graph(object):
    def __init__(self):
        self.inf = 100
        self.n = 5
        self.adj = self.inf*np.ones((self.n, self.n),int)
        self.weight_edges = [(0,1,2),(0,2,1),(1,4,4),(2,3,3),(3,4,5)]#three element group
        self.dist = self.adj
    def set_adj(self):
        for (u,v,weight) in self.weight_edges:
            self.adj[u][v] = self.adj[v][u] = weight
        for i in range(self.n):
            self.adj[i][i] = 0
        print("adjacent matrix of Graph, inf =",self.inf,"\n",self.adj)
        self.dist = self.adj
    def Floyd(self):
        for k in range(self.n):
            for i in range(self.n):
                for j in range(self.n):
                    if self.dist[i][j] > self.dist[i][k] + self.dist[k][j]:
                        self.dist[i][j] = self.dist[i][k] + self.dist[k][j]
        print("dist:\n",self.dist)

                        
    def draw(self):
        G = nx.Graph()
        G.add_weighted_edges_from(self.weight_edges)
        key = range(self.n)
        s = [ str(i+1) for i in range(self.n)]
        s = dict(zip(key, s))
        nx.draw(G, font_weight = 'bold', labels = s)
        plt.figure(2)
        pos = nx.shell_layout(G)
        nx.draw_networkx(G, pos, node_size = 260, labels = s)
        #w = nx.get_edge_atrributes(G, 'weight')
        #nx.draw_network_edge_labels(G,pos, font_size = 12, edge_labels = w)
        plt.show()
G = Graph()
G.set_adj()
G.Floyd()
